/*
 * @Author: zhangchaosen
 * @Date: 2024-05-20 09:29:53
 * @LastEditors: zhangchaosen
 * @LastEditTime: 2024-05-20 10:24:33
 * @Description:
 */
import { ref } from 'vue'
// import { IExportExcel } from '@/api/warning/risk/type'
import { ElMessage } from 'element-plus'
import FileSaver from 'file-saver'

// 公共导出
export const useExport = <T>(pageQuery: ISystemPagination, listQuery: T, fetchApi: Fn, excelTitle: string) => {
  // let data: IExportExcel = {}
  let data: any = {}
  const hasFinish = ref(false)

  const exportFile = async (multipleList: number[]) => {
    if (multipleList.length) {
      data = { idList: multipleList, ...pageQuery }
    } else {
      data = { ...listQuery, ...pageQuery }
    }
    try {
      const res = await fetchApi(data)
      FileSaver.saveAs(res, `${excelTitle}.xlsx`)
      ElMessage.success('导出成功')
      hasFinish.value = true
    } catch (error) {
      hasFinish.value = true
    }
  }

  return { hasFinish, exportFile }
}
